package top.zhangjianyong.tools.reptile;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ： zhangjianyong
 * @date ： 2023/3/22 8:48 AM
 */
public class HtmlUtil {

    public static List<Element> getTag(String html, String tag) {
        Document doc = Jsoup.parse(html);
        Elements elements = doc.getElementsByTag(tag);
        return elements;
    }

    public static List<String> getImgUrl(String html) {
        return getTagAttr(html, "img", "src");
    }

    public static List<String> getLinkUrl(String html) {
        return getTagAttr(html, "a", "href");
    }

    public static List<String> getVideoUrl(String html) {
        return getTagAttr(html, "video", "src");
    }

    public static List<String> getTagAttr(String html, String tag, String attr) {
        Document doc = Jsoup.parse(html);
        return doc.getElementsByTag(tag).eachAttr(attr);
    }

    /**
     * 格式化url
     *
     * @param list
     * @param protocol
     * @param host
     * @param prefix
     * @return
     */
    public static List<String> removeBlankUrl(List<String> list, String protocol, String host, String prefix) {
        return list.stream().filter(s -> {
            if (StringUtils.isBlank(s)) {
                return false;
            }
            s = s.trim();
            if (s.equals("/")) {
                return false;
            }
            if (s.equals("#")) {
                return false;
            }
            if (s.startsWith("javascript")) {
                return false;
            }
            if (s.startsWith("//") || s.startsWith("http") || s.startsWith("/") || s.startsWith("./")) {
                return true;
            }
            System.out.println("不完整地址：" + s);
            return true;
        }).map(s -> {
            if (s.startsWith("//")) {
                s = protocol + ":" + s;
            } else if (s.startsWith("/")) {
                s = protocol + "://" + host + s;
            } else if (s.startsWith("./")) {
                s = prefix + s.substring(1);
            } else if (!s.startsWith("http")) {
                s = protocol + "://" + host + "/" + s;
                System.out.println("拼接协议域名：" + s);
            }
            return s;
        }).collect(Collectors.toList());
    }

    public static List<String> removeBlankUrl(List<String> list, String url) throws UnsupportedEncodingException {
        return removeBlankUrl(list, UrlUtil.protocol(url), UrlUtil.host(url), UrlUtil.prefix(url));
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String url = "https://www.baidu.com";
        String protocol = UrlUtil.protocol(url);
        String host = UrlUtil.host(url);
        HttpResult result = RequestUtil.getHtml(url);
        String html = result.getBody();
        List<String> linkUrl = HtmlUtil.getLinkUrl(html);
        List<String> list = removeBlankUrl(linkUrl, url);
        list.forEach(System.out::println);

        List<String> imgUrl = HtmlUtil.getImgUrl(html);
        List<String> list1 = removeBlankUrl(imgUrl, url);
        list1.forEach(System.out::println);
       /* String source = "https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/qrcode/qrcode@2x-daf987ad02.png?a=12&dd2=22?12?dd=ss";
        System.out.println(source);
        String s = imgName(source, "png");
        System.out.println(s);*/
    }

    public static class HtmlHead {
        public String title;
        public String keywords;
        public String description;

        public HtmlHead(String title, String keywords, String description) {
            this.title = title;
            this.keywords = keywords;
            this.description = description;
        }
    }

    public static HtmlHead getHead(String body) {
        Document doc = Jsoup.parse(body);
        String title = doc.getElementsByTag("title").text();
        String keywords = doc.select("meta[name='keywords']").attr("content");
        String description = doc.select("meta[name='description']").attr("content");
        return new HtmlHead(title, keywords, description);
    }
}
